home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / c-tools / c_examples / button / amigaapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-14  |  3.4 KB  |  140 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // amigaapp.cpp
  3. //
  4. // Jeffry A Worth
  5. // Nov 1, 1995
  6. //////////////////////////////////////////////////////////////////////////////
  7.  
  8. //////////////////////////////////////////////////////////////////////////////
  9. // INCLUDES
  10. #include "aframe:include/amigaapp.hpp"
  11. #include "aframe:include/window.hpp"
  12.  
  13.  
  14. //////////////////////////////////////////////////////////////////////////////
  15. // GLOBAL VARIABLES
  16. struct IntuitionBase *IntuitionBase;
  17. struct GfxBase *GfxBase;
  18. struct ReqToolsBase *ReqToolsBase;
  19. struct Library *LayersBase=NULL;
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23.  
  24. AFAmigaApp::AFAmigaApp()
  25. {
  26.   m_SigBits=NULL;
  27.   appmsgport=NULL;
  28.   ReqToolsBase=NULL;
  29.   DataTypesBase=NULL;
  30.  
  31.   if(!InitApp())
  32.     Exit(0);
  33. }
  34.  
  35. AFAmigaApp::~AFAmigaApp()
  36. {
  37.   CloseLibraries();
  38. }
  39.  
  40. BOOL AFAmigaApp::OpenLibraries()
  41. {
  42.   if(!(IntuitionBase=(struct IntuitionBase*)OpenLibrary((unsigned char*)"intuition.library",(ULONG)39l)))
  43.     return FALSE;
  44.   if(!(GfxBase=(struct GfxBase*)OpenLibrary((unsigned char*)"graphics.library",(ULONG)39l)))
  45.     return FALSE;
  46.   if(!(LayersBase=OpenLibrary((UBYTE*)"layers.library",39l)))
  47.     return FALSE;
  48.   if(!(appmsgport=CreateMsgPort()))
  49.     return FALSE;
  50.   return TRUE;
  51. }
  52.  
  53. void AFAmigaApp::CloseLibraries()
  54. {
  55.   if(GfxBase) CloseLibrary((struct Library*)GfxBase),GfxBase=NULL;
  56.   if(IntuitionBase) CloseLibrary((struct Library*)IntuitionBase),IntuitionBase=NULL;
  57.   if(ReqToolsBase) CloseLibrary((struct Library*)ReqToolsBase),ReqToolsBase=NULL;
  58.   if(DataTypesBase) CloseLibrary((struct Library*)DataTypesBase),DataTypesBase=NULL;
  59.   if(LayersBase) CloseLibrary(LayersBase),LayersBase=NULL;
  60.   if(appmsgport) DeleteMsgPort(appmsgport),appmsgport=NULL;
  61. }
  62.  
  63. int AFAmigaApp::InitApp()
  64. {
  65.   return(OpenLibraries());
  66. }
  67.  
  68. int
  69. AFAmigaApp::RunApp()
  70. {
  71.     AFPtrDlistIterator m_windowiter(m_windows);
  72.     AFPtrDlistIterator m_portiter(m_ports);
  73.     AFWindow* pwindow;
  74.     LPIntuiMessage imess;
  75.     LPAppMessage amess;
  76.     BOOL loop;
  77.  
  78.     while ( (!m_windows.isEmpty()) || (!m_ports.isEmpty()) ) {
  79.     
  80.         // Wait for an event!
  81.         Wait(m_SigBits | 1 << appmsgport->mp_SigBit);
  82.  
  83.         // Loop to look for message that was found
  84.         loop=TRUE;
  85.         while(loop) {
  86.             loop=FALSE;
  87.             m_windowiter.reset();
  88.     
  89.             //prevnode = NULL;
  90.             while(++m_windowiter) {
  91.                 pwindow=(AFWindow*)m_windowiter.key();
  92.         
  93.                 if(pwindow->isValid()) {
  94.  
  95.                     if(imess=pwindow->GetMsg()) {
  96.                         loop=TRUE;
  97.                         pwindow->ExecuteMsg(imess);
  98.  
  99.                         // Reply Message ONLY if Window has not been Destoryed.
  100.                         if(pwindow->isValid())
  101.                             ReplyMsg((LPMessage)imess);
  102.                     }
  103.                     if(amess=pwindow->GetAppMsg(appmsgport)) {
  104.                         loop=TRUE;
  105.                         pwindow->ExecuteAppMsg(amess);
  106.  
  107.                         // Reply Message ONLY if Window has not been Destroyed.
  108.                         if(pwindow->isValid())
  109.                             ReplyMsg((LPMessage)amess);
  110.                     }
  111.                 }
  112.                 if(!pwindow->isValid()) {
  113.                     m_windowiter.removeKey();
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     return(TRUE);
  119. }
  120.  
  121. void
  122. AFAmigaApp::addWindow(AFObject* pwindow)
  123. {
  124.     // Add windows's sigbit
  125.     m_SigBits |= 1<<(((AFWindow*)pwindow)->m_pWindow->UserPort->mp_SigBit);
  126.     
  127.     // Add window to window list
  128.     m_windows.append(pwindow);
  129. }
  130.  
  131. void
  132. AFAmigaApp::removeWindow(AFObject* pwindow)
  133. {
  134.     // Remove window's SigBits
  135.     m_SigBits &= ~(1<<(((AFWindow*)pwindow)->m_pWindow->UserPort->mp_SigBit));
  136.  
  137.     // Remove the window from the window list
  138.     // m_windows.removeNode(pwindow->m_node);
  139. }
  140.